from typing import Any, Dict, List, Optional, Union
from fastapi.responses import JSONResponse
from fastapi import status

def success_response(
    data: Any = None, 
    message: str = "操作成功", 
    status_code: int = status.HTTP_200_OK
) -> JSONResponse:
    """
    成功响应
    
    参数:
        data (Any, optional): 响应数据，默认为None
        message (str, optional): 响应消息，默认为"操作成功"
        status_code (int, optional): HTTP状态码，默认为200
        
    返回:
        JSONResponse: FastAPI JSON响应对象
    """
    content = {
        "success": True,
        "message": message,
        "data": data
    }
    return JSONResponse(content=content, status_code=status_code)

def error_response(
    message: str = "操作失败", 
    status_code: int = status.HTTP_400_BAD_REQUEST,
    errors: Optional[List[Dict[str, Any]]] = None
) -> JSONResponse:
    """
    错误响应
    
    参数:
        message (str, optional): 错误消息，默认为"操作失败"
        status_code (int, optional): HTTP状态码，默认为400
        errors (List[Dict[str, Any]], optional): 错误详情列表，默认为None
        
    返回:
        JSONResponse: FastAPI JSON响应对象
    """
    content = {
        "success": False,
        "message": message,
    }
    
    if errors:
        content["errors"] = errors
        
    return JSONResponse(content=content, status_code=status_code)

def pagination_response(
    data: List[Any],
    total: int,
    page: int = 1,
    page_size: int = 10,
    message: str = "获取数据成功"
) -> JSONResponse:
    """
    分页响应
    
    参数:
        data (List[Any]): 分页数据列表
        total (int): 总记录数
        page (int, optional): 当前页码，默认为1
        page_size (int, optional): 每页记录数，默认为10
        message (str, optional): 响应消息，默认为"获取数据成功"
        
    返回:
        JSONResponse: FastAPI JSON响应对象
    """
    content = {
        "success": True,
        "message": message,
        "data": data,
        "pagination": {
            "total": total,
            "page": page,
            "page_size": page_size,
            "total_pages": (total + page_size - 1) // page_size
        }
    }
    return JSONResponse(content=content) 